package cn.com.surker.workcon.service.fengdu;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.common.Code;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.RandomUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.TaskService;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中联工控机的任务单 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class TaskServiceImpl extends GKJDBCService implements TaskService {

	public int insert(Long lineId, ProductionIndentVo indent, Date datTim) {
		if (lineId == null || indent == null) {
			System.err.println("没有生产线(" + lineId + ")或者任务单信息(" + indent + ")");
			return Code.FAILED.getCode();
		}
		//	获取生产线
		SysLine line = getLine(lineId);
		if (!BaseEntity.validateExist(line)) {
			//	没有数据
			return Code.FAILED.getCode();
		}
		//	获取连接
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		//	任务单编号
		String PTaskListNo = indent.getCode();
		//	根据任务单编号,查询是否存在上传数据
		String queryOldTask = "select id from PTaskList where PTaskListNo = '" + PTaskListNo + "'";
		//	查询数据
		List<Map<String, Object>> oldTasks = lineJdbcTemp.queryForList(queryOldTask);
		if (oldTasks != null && oldTasks.size() > 0 && oldTasks.get(0) != null) {
			//	已经上传过
			return 1;
		}
		//	装配数据,需要先判断是否存在客户,如果没有存在客户,要先插入客户信息
		//	工程名称
		String conName = indent.getConName();
		//	工程地址
		String address = indent.getAddress();
		//	客户名称
		String clientName = indent.getClientName();
		//	工程id
		String projectId = null;
		String queryConSql = "select id from Project where ProjectName = '" + conName + "'";
		List<Map<String, Object>> conList = lineJdbcTemp.queryForList(queryConSql);
		if (conList == null || conList.isEmpty()) {
			//	没有合同需要插入,插入前需要查询客户
			String creator = indent.getCreator();
			if (creator == null) {
				creator = "erp用户";
			}
			String customerId = null;
			String queryClientSql = "select id from Customers where CustomerName = '" + clientName + "'";
			List<Map<String, Object>> cusList = lineJdbcTemp.queryForList(queryClientSql);
			if (cusList == null || cusList.isEmpty()) {
				//	没有客户,需要插入客户数据
				String insertCusSql = "insert into Customers (id,CustomerName,CreateTime,UpdateTime,CreaterID,UpdaterID,Status) values (?,?,?,?,?,?,0)";
				//	客户id
				customerId = RandomUtils.generateUpperString(32);
				Object[] cusParams = {customerId, clientName, datTim, datTim, creator, creator};
				lineJdbcTemp.update(insertCusSql, cusParams);
			} else {
				//	取出客户id
				customerId = String.valueOf(cusList.get(0).get("id"));
			}
			projectId = RandomUtils.generateUpperString(32);
			//	拼接工程参数
			String insertConSql = "insert into Project (id,CustomersID,ProjectName,ProjectAddr,LinkMan,Tel,Distance,Status,CreateTime,UpdateTime,CreaterID,UpdaterID) values (?,?,?,?,?,?,?,0,?,?,?,?)";
			Object[] proParams = {projectId, customerId, conName, address, "", "", 0, datTim, datTim, creator, creator};
			//	插入工程数据
			lineJdbcTemp.update(insertConSql, proParams);
		} else {
			//	取出工程id
			projectId = String.valueOf(conList.get(0).get("id"));
		}
		//	开盘时间
		Date ConstructTime = indent.getDispatchStartTime();
		//	施工部位
		String ConstructPlace = indent.getSite();
		//	计划方量
		Double RequiredNum = indent.getPlanStere();
		// 累计方量
		Double ProvidedNum = 0d;
		// 累计车次
		Double SumCarNum = 0d;
		//	浇筑方式
		String PouringMode = indent.getCastModelName();
		//	强度等级
		String ConcreteStrength = indent.getStrengthName();
		//	塌落度
		String Sink = indent.getSlumps();
		//	完成时间
		Date FinishTime = DateUtil.convertBeforeDayDate(ConstructTime, -2);
		//	拼接插入任务单是sql
		String insertTaskSql = "insert into PTaskList (id,PTaskListNo,ProjectID,ConstructTime,ConstructPlace,RequiredNum,ProvidedNum,SumCarNum,PouringMode,ConcreteStrength,Sink,FinishTime,Version,Status) " +
			"values (?,?,?,?,?,?,?,?,?,?,?,?,1,0)";
		//	拼接参数
		String taskId = RandomUtils.generateUpperString(32);
		;
		Object[] taskParams = {taskId, PTaskListNo, projectId, ConstructTime, ConstructPlace, RequiredNum, ProvidedNum, SumCarNum, PouringMode, ConcreteStrength, Sink, FinishTime};
		//	开启事务
		beginTransaction(lineId);
		//	插入数据
		int update = lineJdbcTemp.update(insertTaskSql, taskParams);
		return update;
	}

}
